
## 根据网页结构解构对象

class Brand:
    # 品牌类
    def __init__(self, html_element):
        self.html = html_element
        self.brand_id = self._parse_brand_id()
        self.brand_name = self._parse_brand_name()
        self.manufactor_list = self._parse_manufactor_list()
    
    def _parse_brand_id(self):
        try:
            return self.html.attrs['id']
        except KeyError:
            return None

    def _parse_brand_name(self):
        brand_name_element = self.html.find('dt')
        try:
            return brand_name_element.text
        except:
            return None

    def _parse_manufactor_list(self):
        manufactor_list = self.html.find_all('ul')
        return [(ul.find_previous('div', attrs={'class': 'h3-tit'}), ul) \
                    for ul in manufactor_list]
    

class Manufactor:
    # 制造工厂类
    def __init__(self, html_element, series_list_html_element):
        self.html = html_element
        self.series_list_html = series_list_html_element
        self.manufactor_name = self._parse_manufactor_name()
        self.series_list = self._parse_series_list()

    def _parse_manufactor_name(self):
        return self.html.text

    def _parse_series_list(self):
        return self.series_list_html.find_all('li')
    
class Series:
    # 车系类
    def __init__(self, html_element):
        self.html = html_element
        self.series_id = self._parse_series_id()
        if self.series_id:
            self.series_name = self._parse_series_name()
            self.series_price = self._parse_series_price()
        else:
            self.series_name = None
            self.series_price = None

    def _parse_series_id(self):
        try:
            return self.html.attrs['id']
        except KeyError:
            return None

    def _parse_series_name(self):
        try:
            return self.html.h4.text
        except:
            print("Error occur when parse ··· {}".format(self.html))

    def _parse_series_price(self):
        try:
            return self.html.find('a', attrs={'class': 'red'}).text
        except AttributeError:
            return None